import sys, xgoogle,cgi,re,os, appengine_utilities
#import multiprocessing

from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
from google.appengine.api import urlfetch
from django.utils import simplejson

from xgoogle.search import GoogleSearch, SearchError, SearchResult
from urlparse import urlparse
from appengine_utilities import sessions

class ValidResult:
	def __init__(self,id,title,desc,url):
		self.id=id
		self.title=title
		self.desc=desc
		self.url=url
		

class MainPage(webapp.RequestHandler):
	def get(self):
		template_variables=''
		path = os.path.join(os.path.dirname(__file__), 'index.html')
		self.response.out.write(template.render(path,template_variables))

class SearchAndAnalyze(webapp.RequestHandler):
	def __init__(self):
		self.session = sessions.Session()
		webapp.RequestHandler.__init__(self)
		self.methods = RPCMethods(self.session)
	def get(self):
		func = None
		self.session['user_agent']=self.request.headers['User-agent']
		self.session['ip']= self.request.remote_addr
    		
		action = self.request.get('action')
		if action:
		      if action[0] == '_':
		      	self.error(403) # access denied
		      	return
		      else:
			func = getattr(self.methods, action, None)
    		if not func:
      			self.error(404) # file not found
      			return
    		args = ()
    		while True:
      			key = 'arg%d' % len(args)
			val = self.request.get(key)
      			if val:
			        args += (simplejson.loads(val),)
			else:
				break
    		result = func(*args)
		template_variables = {
			'results': result
		}
		path = os.path.join(os.path.dirname(__file__), 'response_div.html')
		return simplejson.dumps(self.response.out.write(template.render(path,template_variables)))
	
	def post(self):
		self.response.headers['Content-Type'] = 'text/html'
		keywords=cgi.escape(self.request.get('keywords'))
		target_domain=cgi.escape(self.request.get('domain'))
		target_domain=re.sub("^http://", "", target_domain)
		target_domain=re.sub("^www(\d+)?\.", "", target_domain)

		valid_results = []

		results=self.gsearch(keywords)
		for idx, res in enumerate(results):
			desc=res.desc.encode('utf8')
			parsed_url=urlparse(res.url)
			domain=re.sub("^www(\d+)?\.", "", parsed_url.netloc)
			if domain == target_domain:
				valid_res = ValidResult(idx+1,res.title,res.desc,target_domain)
				valid_results.append(valid_res)
		template_variables = {
			'results': valid_results
		}
		path = os.path.join(os.path.dirname(__file__), 'response.html')
		self.response.out.write(template.render(path,template_variables))
		
class SessionModel(db.Model):
	sid=db.StringProperty()
	user_agent=db.StringProperty()
	ip=db.StringProperty()
	keywords=db.StringProperty()
	domain=db.StringProperty()
	
class RPCMethods:
	def __init__(self, session):
		self.session= session
	def gsearch(self,keywords):
		try:
			gs = GoogleSearch(keywords)
			gs.results_per_page = 50
			results = gs.get_results()
			return results
		except SearchError, e:
			return None
	def matchTargetDomain(self,keywords, target_domain):

		self.session["log_session"]=SessionModel(sid=self.session.sid, user_agent=self.session['user_agent'],ip=self.session['ip'],
							 keywords=keywords, domain=target_domain)
		self.session["log_session"].put()

		valid_results = []
		target_domain=re.sub("^http://", "", target_domain)
		target_domain=re.sub("^www(\d+)?\.", "", target_domain)
		#trim uri 
		target_domain=re.sub("/.*" , "" , target_domain)

		results=self.gsearch(keywords)
		for idx, res in enumerate(results):
			parsed_url=urlparse(res.url)
			domain=re.sub("^http://", "", parsed_url.netloc)
			domain=re.sub("^www(\d+)?\.", "", domain)
			if domain == target_domain:
				valid_res = ValidResult(idx+1,res.title,res.desc,target_domain)
				valid_results.append(valid_res)
		self.session.delete()
		return valid_results	

application = webapp.WSGIApplication(
                                     [('/', MainPage),
				      ('/rpc', SearchAndAnalyze)],
                                     debug=True)

def main():
  run_wsgi_app(application)

if __name__ == "__main__":
  main()

